Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 449)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.40276 11.45563 11.50756 11.55858 11.60871 11.65799 11.70643 11.75407
## [9] 11.80093 11.84704 11.89242 11.93710 11.98110 12.02445 12.06718 12.10925
## [17] 12.15057 12.19113 12.23090 12.26987 12.30801 12.34531 12.38173 12.41726
## [25] 12.45189 12.48557 12.51831 12.55006 12.58082 12.61090 12.64056 12.66971
## [33] 12.69828 12.72618 12.75331 12.77960 12.80495 12.82929 12.85251 12.87455
## [41] 12.89530 12.91469 12.93263 12.94991 12.96724 12.98445 13.00133 13.01770
## [49] 13.03336 13.04811 13.06177 13.07415 13.08505 13.09427 13.10163 13.10694
## [57] 13.11000 13.11050 13.10844 13.10406 13.09760 13.08930 13.07942 13.06820
## [65] 13.05587 13.04269 13.02890 13.01474 13.00046 12.98631 12.97251 12.95637
## [73] 12.93548 12.91055 12.88232 12.85151 12.81884 12.78503 12.75081 12.71691
## [81] 12.68404 12.65293 12.62430 12.59888 12.57739 12.55696 12.53452 12.51047
## [89] 12.48520 12.45913 12.43266 12.40617 12.38009 12.35481 12.33073 12.30826
## [97] 12.28780 12.26974 12.25450 12.24105 12.22815 12.21581 12.20409 12.19302
## [105] 12.18262 12.17294 12.16401 12.15586 12.14854 12.14207 12.13649 12.13288
## [113] 12.13204 12.13358 12.13710 12.14223 12.14857 12.15573 12.16332 12.17095
## [121] 12.17823 12.18477 12.19018 12.19408 12.19606 12.19712 12.19849 12.20015
## [129] 12.20207 12.20425 12.20667 12.20930 12.21213 12.21514 12.21833 12.22165
## [137] 12.22511 12.22868 12.23235 12.23549 12.23766 12.23908 12.23994 12.24049
## [145] 12.24092 12.24146 12.24232 12.24372 12.24587 12.24899 12.25330 12.25901
## [153] 12.26633 12.27550 12.28671 12.30066 12.31759 12.33702 12.35848 12.38149
## [161] 12.40560 12.43032 12.45519 12.47974 12.50349 12.52598 12.54673 12.56527
## [169] 12.58113 12.59736 12.61701 12.63966 12.66486 12.69219 12.72120 12.75147
## [177] 12.78256 12.81403 12.84546 12.87640 12.90643 12.93510 12.96198 12.98664
## [185] 13.00865 13.02756 13.04295 13.05438 13.06141 13.06362 13.06213 13.05844
## [193] 13.05272 13.04509 13.03569 13.02468 13.01218 12.99834 12.98330 12.96721
## [201] 12.95019 12.93241 12.91398 12.89507 12.87197 12.84161 12.80505 12.76331
## [209] 12.71747 12.66855 12.61761 12.56569 12.51385 12.46313 12.41458 12.36924
## [217] 12.32816 12.29240 12.25673 12.21599 12.17127 12.12367 12.07428 12.02419
## [225] 11.97451 11.92634 11.88075 11.83886 11.80176 11.77054 11.74183 11.71177
## [233] 11.68079 11.64930 11.61770 11.58641 11.55583 11.52638 11.49847 11.47250
## [241] 11.44890 11.42806 11.41041 11.39634 11.38572 11.37796 11.37278 11.36993
## [249] 11.36914 11.37014 11.37268 11.37648 11.38127 11.38681 11.39282 11.39903
## [257] 11.40519 11.41102 11.41921 11.43216 11.44916 11.46953 11.49257 11.51760
## [265] 11.54392 11.57084 11.59766 11.62371 11.64828 11.67068 11.69023 11.70622
## [273] 11.72117 11.73787 11.75606 11.77547 11.79583 11.81688 11.83835 11.85997
## [281] 11.88149 11.90263 11.92312 11.94271 11.96112 11.97810 11.99411 12.00985
## [289] 12.02534 12.04061 12.05568 12.07059 12.08536 12.10002 12.11459 12.12912
## [297] 12.14361 12.15811 12.17263 12.18721 12.20144 12.21492 12.22775 12.24002
## [305] 12.25180 12.26320 12.27430 12.28517 12.29592 12.30662 12.31737 12.32825
## [313] 12.33935 12.35075 12.36255 12.37482 12.38822 12.40314 12.41933 12.43651
## [321] 12.45442 12.47279 12.49135 12.50985 12.52801 12.54556 12.56225 12.57779
## [329] 12.59194 12.60441 12.61608 12.62793 12.63986 12.65178 12.66358 12.67518
## [337] 12.68648 12.69737 12.70776 12.71757 12.72668 12.73500 12.74228 12.74840
## [345] 12.75350 12.75771 12.76116 12.76400 12.76635 12.76835 12.77013 12.77183
## [353] 12.77358 12.77551 12.77776 12.78047 12.78358 12.78692 12.79040 12.79395
## [361] 12.79749 12.80094 12.80422 12.80726 12.80996 12.81227 12.81409 12.81535
## [369] 12.81597 12.81587 12.81414 12.81017 12.80434 12.79700 12.78855 12.77933
## [377] 12.76974 12.76013 12.75089 12.74237 12.73496 12.72903 12.72494 12.72307
## [385] 12.72104 12.71673 12.71086 12.70420 12.69748 12.69144 12.68684 12.68440
## [393] 12.68488 12.68848 12.69462 12.70286 12.71276 12.72390 12.73583 12.74813
## [401] 12.76034 12.77205 12.78281 12.79218 12.79974 12.80505 12.80766 12.80902
## [409] 12.81075 12.81275 12.81489 12.81706 12.81912 12.82097 12.82249 12.82355
## [417] 12.82403 12.82381 12.82278 12.82082 12.81779 12.81409 12.81012 12.80588
## [425] 12.80135 12.79652 12.79135 12.78585 12.77998 12.77374 12.76710 12.76005
## [433] 12.75257 12.74463 12.73624 12.72736 12.71798 12.70808 12.69764 12.68666
## [441] 12.67510 12.66270 12.64929 12.63499 12.61990 12.60415 12.58785 12.57112
## [449] 12.55407
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 449)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.83101 10.91081 10.98926 11.06632 11.14198 11.21620 11.28896 11.36023
## [9] 11.42999 11.49821 11.56486 11.62992 11.69335 11.75514 11.81526 11.87374
## [17] 11.93067 11.98605 12.03992 12.09230 12.14321 12.19267 12.24071 12.28735
## [25] 12.33262 12.37652 12.41910 12.46037 12.50035 12.53873 12.57524 12.60995
## [33] 12.64297 12.67437 12.70423 12.73266 12.75972 12.78551 12.81012 12.83362
## [41] 12.85611 12.87767 12.89839 12.91716 12.93301 12.94625 12.95715 12.96601
## [49] 12.97314 12.97880 12.98331 12.98696 12.99002 12.99281 12.99560 12.99870
## [57] 13.00239 13.00557 13.00701 13.00689 13.00536 13.00258 12.99871 12.99390
## [65] 12.98831 12.98211 12.97544 12.96847 12.96136 12.95425 12.94732 12.94011
## [73] 12.93209 12.92330 12.91378 12.90358 12.89273 12.88127 12.86925 12.85671
## [81] 12.84368 12.83021 12.81635 12.80212 12.78758 12.77190 12.75441 12.73534
## [89] 12.71491 12.69336 12.67092 12.64782 12.62429 12.60056 12.57687 12.55345
## [97] 12.53052 12.50831 12.48707 12.46318 12.43369 12.39980 12.36271 12.32363
## [105] 12.28376 12.24430 12.20645 12.17141 12.14039 12.11459 12.09521 12.07881
## [113] 12.06138 12.04324 12.02474 12.00620 11.98796 11.97036 11.95372 11.93839
## [121] 11.92470 11.91298 11.90357 11.89680 11.89300 11.89186 11.89271 11.89539
## [129] 11.89975 11.90563 11.91286 11.92128 11.93075 11.94108 11.95214 11.96375
## [137] 11.97576 11.98801 12.00033 12.01475 12.03308 12.05486 12.07961 12.10686
## [145] 12.13616 12.16704 12.19901 12.23163 12.26442 12.29692 12.32865 12.35915
## [153] 12.38795 12.41459 12.43860 12.46305 12.49094 12.52166 12.55459 12.58915
## [161] 12.62471 12.66068 12.69645 12.73141 12.76496 12.79650 12.82541 12.85110
## [169] 12.87295 12.89325 12.91455 12.93664 12.95935 12.98247 13.00581 13.02918
## [177] 13.05238 13.07522 13.09750 13.11903 13.13962 13.15907 13.17718 13.19377
## [185] 13.20864 13.22160 13.23244 13.24098 13.24703 13.25038 13.25215 13.25347
## [193] 13.25419 13.25415 13.25321 13.25120 13.24799 13.24341 13.23732 13.22955
## [201] 13.21997 13.20842 13.19474 13.17878 13.15860 13.13287 13.10233 13.06775
## [209] 13.02988 12.98948 12.94731 12.90411 12.86065 12.81768 12.77596 12.73625
## [217] 12.69930 12.66587 12.63165 12.59250 12.54939 12.50327 12.45513 12.40594
## [225] 12.35665 12.30826 12.26171 12.21799 12.17806 12.14290 12.10847 12.07056
## [233] 12.02984 11.98700 11.94272 11.89767 11.85253 11.80799 11.76472 11.72341
## [241] 11.68472 11.64935 11.61797 11.59126 11.56747 11.54445 11.52231 11.50117
## [249] 11.48114 11.46231 11.44479 11.42871 11.41415 11.40124 11.39008 11.38078
## [257] 11.37344 11.36818 11.36602 11.36762 11.37255 11.38035 11.39058 11.40280
## [265] 11.41655 11.43140 11.44690 11.46260 11.47807 11.49284 11.50649 11.51856
## [273] 11.53109 11.54620 11.56355 11.58283 11.60369 11.62581 11.64885 11.67250
## [281] 11.69641 11.72026 11.74372 11.76646 11.78814 11.80845 11.82959 11.85369
## [289] 11.88029 11.90891 11.93907 11.97031 12.00216 12.03414 12.06578 12.09662
## [297] 12.12617 12.15396 12.17954 12.20241 12.22379 12.24516 12.26648 12.28770
## [305] 12.30878 12.32967 12.35034 12.37075 12.39084 12.41057 12.42991 12.44880
## [313] 12.46721 12.48509 12.50241 12.51910 12.53533 12.55124 12.56685 12.58216
## [321] 12.59717 12.61189 12.62631 12.64043 12.65428 12.66783 12.68110 12.69409
## [329] 12.70681 12.71925 12.73121 12.74252 12.75328 12.76355 12.77342 12.78295
## [337] 12.79223 12.80134 12.81034 12.81933 12.82837 12.83754 12.84628 12.85408
## [345] 12.86104 12.86731 12.87301 12.87826 12.88319 12.88793 12.89260 12.89733
## [353] 12.90224 12.90747 12.91313 12.91936 12.92613 12.93331 12.94080 12.94852
## [361] 12.95638 12.96431 12.97221 12.98000 12.98760 12.99493 13.00189 13.00840
## [369] 13.01438 13.01975 13.02440 13.02838 13.03178 13.03470 13.03724 13.03952
## [377] 13.04162 13.04365 13.04571 13.04791 13.05035 13.05312 13.05633 13.06008
## [385] 13.06424 13.06856 13.07295 13.07736 13.08171 13.08594 13.08997 13.09374
## [393] 13.09717 13.10095 13.10564 13.11104 13.11695 13.12315 13.12944 13.13560
## [401] 13.14143 13.14671 13.15125 13.15482 13.15722 13.15825 13.15768 13.15621
## [409] 13.15461 13.15283 13.15083 13.14854 13.14592 13.14291 13.13946 13.13553
## [417] 13.13105 13.12597 13.12025 13.11383 13.10665 13.09888 13.09072 13.08213
## [425] 13.07313 13.06370 13.05383 13.04351 13.03274 13.02150 13.00978 12.99758
## [433] 12.98489 12.97169 12.95799 12.94376 12.92901 12.91372 12.89788 12.88148
## [441] 12.86452 12.84688 12.82850 12.80941 12.78969 12.76937 12.74850 12.72716
## [449] 12.70537
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 449)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.86332 10.92270 10.98109 11.03848 11.09486 11.15024 11.20460 11.25795
## [9] 11.31028 11.36158 11.41185 11.46109 11.50929 11.55645 11.60256 11.64766
## [17] 11.69178 11.73491 11.77705 11.81820 11.85834 11.89747 11.93560 11.97270
## [25] 12.00878 12.04383 12.07785 12.11083 12.14276 12.17354 12.20309 12.23144
## [33] 12.25863 12.28470 12.30969 12.33363 12.35658 12.37856 12.39961 12.41977
## [41] 12.43908 12.45759 12.47532 12.49216 12.50800 12.52285 12.53673 12.54966
## [49] 12.56167 12.57277 12.58299 12.59234 12.60084 12.60852 12.61539 12.62148
## [57] 12.62681 12.63120 12.63453 12.63681 12.63808 12.63838 12.63774 12.63620
## [65] 12.63378 12.63053 12.62648 12.62165 12.61609 12.60984 12.60291 12.59380
## [73] 12.58127 12.56583 12.54798 12.52820 12.50700 12.48487 12.46230 12.43980
## [81] 12.41787 12.39698 12.37765 12.36037 12.34564 12.33144 12.31563 12.29850
## [89] 12.28032 12.26140 12.24202 12.22245 12.20300 12.18394 12.16556 12.14816
## [97] 12.13201 12.11740 12.10462 12.09282 12.08099 12.06921 12.05753 12.04603
## [105] 12.03478 12.02383 12.01327 12.00314 11.99353 11.98450 11.97611 11.96873
## [113] 11.96258 11.95748 11.95327 11.94978 11.94686 11.94433 11.94203 11.93980
## [121] 11.93747 11.93487 11.93184 11.92822 11.92383 11.91868 11.91298 11.90685
## [129] 11.90046 11.89394 11.88744 11.88110 11.87508 11.86950 11.86453 11.86030
## [137] 11.85695 11.85464 11.85351 11.85211 11.84915 11.84493 11.83980 11.83408
## [145] 11.82809 11.82217 11.81665 11.81185 11.80811 11.80574 11.80509 11.80646
## [153] 11.81021 11.81665 11.82611 11.83886 11.85469 11.87318 11.89392 11.91648
## [161] 11.94047 11.96546 11.99105 12.01682 12.04235 12.06723 12.09105 12.11340
## [169] 12.13386 12.15570 12.18209 12.21251 12.24644 12.28335 12.32271 12.36400
## [177] 12.40670 12.45027 12.49419 12.53794 12.58098 12.62280 12.66287 12.70066
## [185] 12.73565 12.76730 12.79510 12.81852 12.83703 12.85011 12.86000 12.86921
## [193] 12.87761 12.88508 12.89149 12.89671 12.90061 12.90307 12.90395 12.90313
## [201] 12.90048 12.89588 12.88919 12.88029 12.86615 12.84456 12.81657 12.78326
## [209] 12.74570 12.70495 12.66207 12.61815 12.57423 12.53140 12.49071 12.45323
## [217] 12.42004 12.39219 12.36428 12.33091 12.29322 12.25235 12.20943 12.16559
## [225] 12.12196 12.07968 12.03988 12.00368 11.97224 11.94667 11.92403 11.90075
## [233] 11.87705 11.85316 11.82929 11.80568 11.78252 11.76006 11.73851 11.71809
## [241] 11.69902 11.68152 11.66582 11.65213 11.64069 11.63144 11.62415 11.61858
## [249] 11.61451 11.61171 11.60995 11.60899 11.60862 11.60859 11.60867 11.60865
## [257] 11.60828 11.60733 11.60689 11.60808 11.61072 11.61466 11.61974 11.62579
## [265] 11.63266 11.64018 11.64819 11.65652 11.66503 11.67354 11.68189 11.68992
## [273] 11.69831 11.70772 11.71801 11.72905 11.74069 11.75280 11.76522 11.77783
## [281] 11.79047 11.80301 11.81531 11.82721 11.83860 11.84931 11.86012 11.87181
## [289] 11.88426 11.89734 11.91095 11.92496 11.93926 11.95372 11.96823 11.98267
## [297] 11.99691 12.01085 12.02437 12.03734 12.05111 12.06692 12.08444 12.10339
## [305] 12.12344 12.14428 12.16562 12.18714 12.20854 12.22951 12.24973 12.26890
## [313] 12.28672 12.30288 12.31706 12.32896 12.33888 12.34744 12.35478 12.36107
## [321] 12.36646 12.37111 12.37517 12.37881 12.38217 12.38541 12.38870 12.39218
## [329] 12.39602 12.40037 12.40479 12.40879 12.41240 12.41567 12.41863 12.42133
## [337] 12.42380 12.42610 12.42825 12.43030 12.43230 12.43428 12.43586 12.43669
## [345] 12.43687 12.43652 12.43573 12.43461 12.43326 12.43178 12.43028 12.42885
## [353] 12.42762 12.42666 12.42610 12.42603 12.42543 12.42338 12.42012 12.41589
## [361] 12.41093 12.40549 12.39981 12.39413 12.38869 12.38375 12.37954 12.37630
## [369] 12.37428 12.37372 12.37412 12.37479 12.37575 12.37699 12.37850 12.38030
## [377] 12.38237 12.38472 12.38734 12.39024 12.39341 12.39686 12.40057 12.40456
## [385] 12.40970 12.41656 12.42469 12.43364 12.44294 12.45216 12.46083 12.46849
## [393] 12.47471 12.48085 12.48843 12.49714 12.50672 12.51686 12.52729 12.53772
## [401] 12.54786 12.55743 12.56613 12.57368 12.57980 12.58420 12.58660 12.58788
## [409] 12.58909 12.59019 12.59112 12.59182 12.59223 12.59231 12.59199 12.59122
## [417] 12.58995 12.58812 12.58568 12.58257 12.57873 12.57434 12.56961 12.56451
## [425] 12.55904 12.55319 12.54695 12.54030 12.53324 12.52575 12.51783 12.50946
## [433] 12.50062 12.49132 12.48153 12.47126 12.46048 12.44918 12.43736 12.42500
## [441] 12.41210 12.39853 12.38423 12.36925 12.35365 12.33749 12.32082 12.30370
## [449] 12.28619
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")